package com.atguigu.gmall.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 内部使用的controller
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年06月30日 16:09
 * @ClassName:ItemController
 * @Description: TODO
 */
@RestController
@RequestMapping(value = "/admin/product/item")
public class ItemController {


    @Autowired
    private ItemService itemService;

    /**
     * 根据skuId获取sku信息
     * @param skuId
     * @return
     */
    @GmallCache(prefix = "sku_",suffix = "_info")
    @GetMapping(value = "/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable(value = "skuId") Long skuId){
        return itemService.getSkuInfoDBOrRedis(skuId);
    }

    /**
     * 通过三级分类id查询分类信息
     * @param cId
     * @return
     */
    @GmallCache(prefix = "category_",suffix = "_info")
    @GetMapping(value = "/getCategory/{cId}")
    public BaseCategoryView getCategory(@PathVariable("cId")Long cId){
        return itemService.getCategory(cId);
    }
    /**
     * 获取sku价格
     * @param skuId
     * @return
     */
    @GmallCache(prefix = "price_",suffix = "_info")
    @GetMapping(value = "/getPrice/{skuId}")
    public BigDecimal getSkuPrice(@PathVariable(value = "skuId") Long skuId){
        return  itemService.getPrice(skuId);
    }

    /**
     *通过skuid和spuid查询当前商品的销售属性和当前spu的所有的销售属性
     * @param skuId
     * @param spuId
     * @return
     */
    @GmallCache(prefix = "saleAttrBySpuAndSku_",suffix = "_info")
    @GetMapping("/getSaleAttrBySpuAndSku/{skuId}/{spuId}")
    public List<SpuSaleAttr> getSaleAttrBySpuAndSku(@PathVariable(value = "skuId") Long skuId,@PathVariable(value = "spuId") Long spuId){
     return  itemService.getSaleAttrBySpuAndSku(skuId,spuId);
    }
    /**
     * 根据spuid查询出该spu下所有的sku与销售属性值的对应关系
     * @param spuId
     * @return
     */
    @GmallCache(prefix = "skuSaleAttrListBySpuId_",suffix = "_info")
    @GetMapping(value = "/getSkuSaleAttrListBySpuId/{spuId}")
    public Map getSkuSaleAttrListBySpuId(@PathVariable(value = "spuId")Long spuId) {

        return itemService.getSkuSaleAttrListBySpuId(spuId);
    }

    /**
     * 查询首页信息
     * @return
     */
    @GmallCache(prefix = "getCategoryInfoList")
    @GetMapping("/getCategoryInfoList")
    public  List<JSONObject> getCategoryInfoList(){
        return itemService.getCategoryInfoList();
    }

    /**
     * 查询品牌信息详情
     * @return
     */
    @GmallCache(prefix = "baseTrademark")
    @GetMapping(value = "/getById/{id}")
    public BaseTrademark getById(@PathVariable(value = "id") Long id){
        return itemService.getById(id);
    }
    /**
     * 根据sku的id查询平台属性信息
     * @param skuId
     * @return
     */
    @GmallCache(prefix = "baseAttrInfoBySku")
    @GetMapping(value = "/selectBaseAttrInfoBySku/{skuId}")
    public List<BaseAttrInfo> selectBaseAttrInfoBySku(@PathVariable(value = "skuId")Long skuId){
     return itemService.selectBaseAttrInfoBySku(skuId);
    }
}
